package com.attcatstudy.cas;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author attcat
 * ---做一个有趣的程序员
 * @date 2022-11-02 20:33
 * @description
 */
public class AtomicReferenceTest {



    public static void main(String[] args) {
        User user1 = new User("张三", 23);
        User user2 = new User("李四", 25);
        User user3 = new User("王五", 20);
        AtomicReference<User> atomicReference = new AtomicReference<>();
        atomicReference.set(user1);
        //user 1替换user 2
        atomicReference.compareAndSet(user1, user2);
        System.out.println(atomicReference.get());
        //user 1替换 user3  但是这个时候是user2
        atomicReference.compareAndSet(user1, user3);
        System.out.println(atomicReference.get());



    }


}

